//
//  HXLanguageViewController.m
//  HongxuanBluetooth
//
//  Created by 广州弘轩科技发展有限公司 on 2023/8/24.
//

#import "HXLanguageViewController.h"

@interface HXLanguageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSIndexPath *lastPath;// 上一个选中的cll的indexPath
    UITableView *tableview;
    
    
    
}
@property (nonatomic,strong) UIImageView *mainscreen;
@property (nonatomic,strong) UIImageView *statebj;
@end

@implementation HXLanguageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.fd_prefersNavigationBarHidden = YES;
    [self BottomFunction];
//    [self languagetable];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changelanguages) name:@"changelanguages" object:nil];
}
//-(void)changelanguages{
//
//}
-(void)BottomFunction{
    
    UIImageView *tabr = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight-44, winWidth, 44)];
    [self.view addSubview:tabr];
    UIImageView *mainscreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 44)];
//    language midnight
    if([themeColors isEqualToString:@"default"]){
        mainscreen.image = [UIImage imageNamed:@"languagemian"];
    }else{
        mainscreen.image = [UIImage imageNamed:@"languagemidnight"];
    }
//    mainscreen.image = [UIImage imageNamed:@"languagemian"];
    self.mainscreen = mainscreen;
    [tabr addSubview:mainscreen];
    
    UIImageView *statebj = [[UIImageView alloc] init];
    if([themeColors isEqualToString:@"default"]){
        statebj.backgroundColor = [UIColor blackColor];
    }else{
        statebj.backgroundColor = midnightRGB;
    }
    self.statebj = statebj;
    [self.view addSubview:statebj];
    [statebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-34);
        make.height.mas_equalTo(50);
    }];
    
#pragma mark 返回入口
    UIImageView *backview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back"]];
    backview.userInteractionEnabled = YES;
    [self.view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(36);
        make.left.mas_equalTo(26);
    }];
    
    UIButton *backbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:backbt];
    [backbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(backview);
    }];
    
    UIImageView *bluetherview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bluether"]];
    bluetherview.userInteractionEnabled = YES;
    [self.view addSubview:bluetherview];
    [bluetherview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-52);
        make.height.mas_equalTo(13);
    }];
    [self addlangyage];
}
- (void)addlangyage{
//    简体中文
    UIImageView *cnview = [[UIImageView alloc] init];
    cnview.userInteractionEnabled = YES;
    cnview.layer.cornerRadius = 8;
    cnview.layer.masksToBounds = YES;
    if([themeColors isEqualToString:@"default"]){
        cnview.image = [UIImage imageNamed:@"languagekuang"];
    }else{
        cnview.backgroundColor = midnightRGB;
    }
//    cnview.image = [UIImage imageNamed:@"languagekuang"];
    [self.view addSubview:cnview];
    [cnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(kNavHeight+24);
        make.height.mas_equalTo(46.33);
    }];
    
    UIImageView *china = [[UIImageView alloc] init];
    china.image = [UIImage imageNamed:@"china"];
    [cnview addSubview:china];
    [china mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(24);
        make.width.height.mas_equalTo(20.33);
    }];
    
    UILabel *chinalb = [[UILabel alloc] init];
    chinalb.text = @"简体中文";
    chinalb.textColor = [UIColor whiteColor];
    chinalb.textAlignment = NSTextAlignmentLeft;
    chinalb.font = [UIFont systemFontOfSize:25];
    [cnview addSubview:chinalb];
    [chinalb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(11);
        make.left.mas_equalTo(china.mas_right).offset(15);
        make.height.mas_equalTo(22);
    }];
    
    UIButton *chinabt = [UIButton buttonWithType:UIButtonTypeCustom];
    chinabt.tag = 1;
    [cnview addSubview:chinabt];
    [chinabt addTarget:self action:@selector(selectbt:) forControlEvents:UIControlEventTouchUpInside];
    [chinabt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(cnview);
    }];
    
    
    //    英文
    UIImageView *usview = [[UIImageView alloc] init];
    usview.userInteractionEnabled = YES;
    usview.layer.cornerRadius = 8;
    usview.layer.masksToBounds = YES;
    usview.image = [UIImage imageNamed:@"languagekuang"];
    usview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:usview];
    [usview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(cnview.mas_bottom).offset(12.33);
        make.height.mas_equalTo(46.33);
    }];
    
    UIImageView *us = [[UIImageView alloc] init];
    us.image = [UIImage imageNamed:@"English"];
    [usview addSubview:us];
    [us mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(24);
        make.width.height.mas_equalTo(20.33);
    }];

    UILabel *uslb = [[UILabel alloc] init];
    uslb.text = @"English";
    uslb.textColor = [UIColor blackColor];
    uslb.textAlignment = NSTextAlignmentLeft;
    uslb.font = [UIFont systemFontOfSize:25];
    [usview addSubview:uslb];
    [uslb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(china.mas_right).offset(15);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *uslbt = [UIButton buttonWithType:UIButtonTypeCustom];
    uslbt.tag = 2;
    [usview addSubview:uslbt];
    [uslbt addTarget:self action:@selector(selectbt:) forControlEvents:UIControlEventTouchUpInside];
    [uslbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(usview);
    }];
    
//    繁体
    UIImageView *zhtwview = [[UIImageView alloc] init];
    zhtwview.userInteractionEnabled = YES;
    zhtwview.layer.cornerRadius = 8;
    zhtwview.layer.masksToBounds = YES;
    if([themeColors isEqualToString:@"default"]){
        zhtwview.image = [UIImage imageNamed:@"languagekuang"];
    }else{
        zhtwview.backgroundColor = midnightRGB;
    }
//    zhtwview.image = [UIImage imageNamed:@"languagekuang"];
    [self.view addSubview:zhtwview];
    [zhtwview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(usview.mas_bottom).offset(12.33);
        make.height.mas_equalTo(46.33);
    }];
    
    UIImageView *zhtw = [[UIImageView alloc] init];
    zhtw.image = [UIImage imageNamed:@"china"];
    [zhtwview addSubview:zhtw];
    [zhtw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(24);
        make.width.height.mas_equalTo(20.33);
    }];
    
    UILabel *zhtwlb = [[UILabel alloc] init];
    zhtwlb.text = @"繁体中文";
    zhtwlb.textColor = [UIColor whiteColor];
    zhtwlb.textAlignment = NSTextAlignmentLeft;
    zhtwlb.font = [UIFont systemFontOfSize:25];
    [zhtwview addSubview:zhtwlb];
    [zhtwlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(zhtw.mas_right).offset(15);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *zhtwbt = [UIButton buttonWithType:UIButtonTypeCustom];
    zhtwbt.tag = 3;
    [zhtwview addSubview:zhtwbt];
    [zhtwbt addTarget:self action:@selector(selectbt:) forControlEvents:UIControlEventTouchUpInside];
    [zhtwbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(usview);
    }];
    
//    日语
    UIImageView *japaneseview = [[UIImageView alloc] init];
    japaneseview.userInteractionEnabled = YES;
    japaneseview.layer.cornerRadius = 8;
    japaneseview.layer.masksToBounds = YES;
    if([themeColors isEqualToString:@"default"]){
        japaneseview.image = [UIImage imageNamed:@"languagekuang"];
    }else{
        japaneseview.backgroundColor = midnightRGB;
    }
//    japaneseview.image = [UIImage imageNamed:@"languagekuang"];
    [self.view addSubview:japaneseview];
    [japaneseview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(zhtwview.mas_bottom).offset(12.33);
        make.height.mas_equalTo(46.33);
    }];
    
    UIImageView *japanese = [[UIImageView alloc] init];
    japanese.image = [UIImage imageNamed:@"japanese"];
    [japaneseview addSubview:japanese];
    [japanese mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(24);
        make.width.height.mas_equalTo(20.33);
    }];
    
    UILabel *japaneselb = [[UILabel alloc] init];
    japaneselb.text = @"日本語";
    japaneselb.textColor = [UIColor whiteColor];
    japaneselb.textAlignment = NSTextAlignmentLeft;
    japaneselb.font = [UIFont systemFontOfSize:25];
    [japaneseview addSubview:japaneselb];
    [japaneselb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(japanese.mas_right).offset(15);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *japanesebt = [UIButton buttonWithType:UIButtonTypeCustom];
    japanesebt.tag = 4;
    [japaneseview addSubview:japanesebt];
    [japanesebt addTarget:self action:@selector(selectbt:) forControlEvents:UIControlEventTouchUpInside];
    [japanesebt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(japaneseview);
    }];
    
//    Deutsch 德语
    UIImageView *deutschview = [[UIImageView alloc] init];
    deutschview.userInteractionEnabled = YES;
    deutschview.layer.cornerRadius = 8;
    deutschview.layer.masksToBounds = YES;
    if([themeColors isEqualToString:@"default"]){
        deutschview.image = [UIImage imageNamed:@"languagekuang"];
    }else{
        deutschview.backgroundColor = midnightRGB;
    }
//    deutschview.image = [UIImage imageNamed:@"languagekuang"];
    [self.view addSubview:deutschview];
    [deutschview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(japaneseview.mas_bottom).offset(12.33);
        make.height.mas_equalTo(46.33);
    }];
    
    UIImageView *deutsch = [[UIImageView alloc] init];
    deutsch.image = [UIImage imageNamed:@"deutsch"];
    [deutschview addSubview:deutsch];
    [deutsch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(24);
        make.width.height.mas_equalTo(20.33);
    }];
    
    UILabel *deutschlb = [[UILabel alloc] init];
    deutschlb.text = @"Deutsch";
    deutschlb.textColor = [UIColor whiteColor];
    deutschlb.textAlignment = NSTextAlignmentLeft;
    deutschlb.font = [UIFont systemFontOfSize:25];
    [deutschview addSubview:deutschlb];
    [deutschlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(japanese.mas_right).offset(15);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *deutschbt = [UIButton buttonWithType:UIButtonTypeCustom];
    deutschbt.tag = 5;
    [deutschview addSubview:deutschbt];
    [deutschbt addTarget:self action:@selector(selectbt:) forControlEvents:UIControlEventTouchUpInside];
    [deutschbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(deutschview);
    }];
    
//    Русский язык
    UIImageView *Русскийязыкview = [[UIImageView alloc] init];
    Русскийязыкview.userInteractionEnabled = YES;
    Русскийязыкview.layer.cornerRadius = 8;
    Русскийязыкview.layer.masksToBounds = YES;
    if([themeColors isEqualToString:@"default"]){
        Русскийязыкview.image = [UIImage imageNamed:@"languagekuang"];
    }else{
        Русскийязыкview.backgroundColor = midnightRGB;
    }
//    Русскийязыкview.image = [UIImage imageNamed:@"languagekuang"];
    [self.view addSubview:Русскийязыкview];
    [Русскийязыкview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(deutschview.mas_bottom).offset(12.33);
        make.height.mas_equalTo(46.33);
    }];
    
    UIImageView *Русскийязык = [[UIImageView alloc] init];
    Русскийязык.image = [UIImage imageNamed:@"Русскийязык"];
    [Русскийязыкview addSubview:Русскийязык];
    [Русскийязык mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(24);
        make.width.height.mas_equalTo(20.33);
    }];
    
    UILabel *Русскийязыкlb = [[UILabel alloc] init];
    Русскийязыкlb.text = @"Русскийязык";
    Русскийязыкlb.textColor = [UIColor whiteColor];
    Русскийязыкlb.textAlignment = NSTextAlignmentLeft;
    Русскийязыкlb.font = [UIFont systemFontOfSize:25];
    [Русскийязыкview addSubview:Русскийязыкlb];
    [Русскийязыкlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(Русскийязык.mas_right).offset(15);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *Русскийязыкbt = [UIButton buttonWithType:UIButtonTypeCustom];
    Русскийязыкbt.tag = 6;
    [Русскийязыкview addSubview:Русскийязыкbt];
    [Русскийязыкbt addTarget:self action:@selector(selectbt:) forControlEvents:UIControlEventTouchUpInside];
    [Русскийязыкbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(deutschview);
    }];
    
//    Italiano
    
    UIImageView *Italianoview = [[UIImageView alloc] init];
    Italianoview.userInteractionEnabled = YES;
    Italianoview.layer.cornerRadius = 8;
    Italianoview.layer.masksToBounds = YES;
    if([themeColors isEqualToString:@"default"]){
        Italianoview.image = [UIImage imageNamed:@"languagekuang"];
    }else{
        Italianoview.backgroundColor = midnightRGB;
    }
//    Italianoview.image = [UIImage imageNamed:@"languagekuang"];
    [self.view addSubview:Italianoview];
    [Italianoview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(Русскийязыкview.mas_bottom).offset(12.33);
        make.height.mas_equalTo(46.33);
    }];
    
    UIImageView *Italiano = [[UIImageView alloc] init];
    Italiano.image = [UIImage imageNamed:@"Italiano"];
    [Italianoview addSubview:Italiano];
    [Italiano mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(24);
        make.width.height.mas_equalTo(20.33);
    }];
    
    UILabel *Italianolb = [[UILabel alloc] init];
    Italianolb.text = @"Italiano";
    Italianolb.textColor = [UIColor whiteColor];
    Italianolb.textAlignment = NSTextAlignmentLeft;
    Italianolb.font = [UIFont systemFontOfSize:25];
    [Italianoview addSubview:Italianolb];
    [Italianolb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(Italiano.mas_right).offset(15);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *Italianobt = [UIButton buttonWithType:UIButtonTypeCustom];
    Italianobt.tag = 7 ;
    [Italianoview addSubview:Italianobt];
    [Italianobt addTarget:self action:@selector(selectbt:) forControlEvents:UIControlEventTouchUpInside];
    [Italianobt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(Italianoview);
    }];
}
- (void)languagetable{
    
//    默认选择
    lastPath = [NSIndexPath indexPathForRow:3 inSection:0];
    
    UITableView *table = [[UITableView alloc] init];
    table.backgroundColor = [UIColor blackColor];
//    tab.transform = CGAffineTransformMakeRotation(M_PI/-2);
//    table.frame = CGRectMake(0, 80, self.view.bounds.size.width, self.view.bounds.size.height);
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
//    table.showsVerticalScrollIndicator = NO;
    table.rowHeight = 59;
//    self.automaticallyAdjustsScrollViewInsets = false;
    tableview = table;
    [self.view addSubview:table];
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(kNavHeight+24);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37-36);
    }];
    [table layoutIfNeeded];
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

#pragma mark - InitCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//    languagekuang
    //if (cell == nil)
    //{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] ;
    
    // 标记选中的声音
    NSInteger row = [indexPath row];
    NSInteger oldRow = [lastPath row];
    
    UIImageView *languagekuang = [[UIImageView alloc] init];
    languagekuang.image = [UIImage imageNamed:@"languagekuang"];
    languagekuang.frame = CGRectMake(0, 6, cell.bounds.size.width, 46.33);
    cell.accessoryView = languagekuang;
    
    
//    UIButton *headButton = [[UIButton alloc] init];
////    headButton.transform = CGAffineTransformMakeRotation(M_PI/2);
//    headButton.frame = CGRectMake(0, 6, winWidth -60, 60);
//    headButton.layer.masksToBounds = YES;
//    headButton.layer.cornerRadius = 8;
//    headButton.layer.borderWidth = 1.0;
//    headButton.layer.borderColor = [UIColor whiteColor].CGColor;
    
    
    cell.accessoryView = languagekuang;
    cell.layer.cornerRadius = 8;
    cell.layer.borderWidth = 1.0;
    cell.layer.borderColor = [UIColor whiteColor].CGColor;
    
//    headButton.userInteractionEnabled = NO;
//    headButton.backgroundColor = [UIColor blackColor];
    
    if (row == oldRow)
    {
//        headButton.backgroundColor = [UIColor whiteColor];
        languagekuang.image = [UIImage imageNamed:@"languagekuang"];
    }
    
    cell.backgroundColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //}
    return cell;
}
- (void)selectbt:(UIButton *)button{
    
    NSLog(@"点击了按钮%ld",button.tag);
    if(button.tag !=2){
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"Other languages to be developed" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //        [self installAppWithModel:model];
            NSLog(@"点击了yes");
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVc addAction:sureAction];
        [alertVc addAction:cancelAction];
        [self presentViewController:alertVc animated:YES completion:nil];
    }
}
#pragma mark - SelectCell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger newRow = [indexPath row];
    
    NSInteger oldRow = (lastPath !=nil)?[lastPath row]:-1;
    
    if (newRow != oldRow)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryView.backgroundColor = [UIColor whiteColor];
        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastPath];
        oldCell.accessoryView.backgroundColor = [UIColor blackColor];
        
        lastPath = indexPath;
        NSLog(@"%ld",(long)indexPath.row);
    }
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)back{
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}



@end
